package com.wsc.personalblog.controller;

import com.wsc.personalblog.DTO.FileDto;
import com.wsc.personalblog.DTO.LeaveAMsgDTO;
import com.wsc.personalblog.utils.AliYunOssUtil;
import com.wsc.personalblog.common.Result;
import com.wsc.personalblog.entity.LeaveAMessage;
import com.wsc.personalblog.service.LeaveAMessageService;
import com.wsc.personalblog.service.impl.LikeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Api("留言相关的操作")
@RestController
@RequestMapping("/leaveAMessage")
public class LeaveAMessageController {

    @Autowired
    private LeaveAMessageService leaveAMessageService;
    @Autowired
    private LikeServiceImpl likeService;



    @PostMapping
    @ApiOperation("发布留言")
//    @ApiImplicitParams(
//            {@ApiImplicitParam(name = "leaveAMessage", dataType = "LeaveAMessage",required = true, value = "存放参数的对象"),})
    public Result<String> pubLAM(
            @RequestBody LeaveAMessage leaveAMessage){
        leaveAMessageService.pubLAM(leaveAMessage);
        return Result.successNoData();
    }

    @DeleteMapping
    @ApiOperation("递归删除留言")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "LAMIds", dataType = "List",required = true, value = "待删除评论id集合:主评论及其子评论id集")})
    public Result<String> removeLAMByIds(
            @RequestBody List<Integer> LAMIds){
        leaveAMessageService.removeLAMByIds(LAMIds);
        return Result.successNoData();
    }

    @GetMapping("/public/{userId}")
    @ApiOperation("获取全部留言")
    public Result<List<LeaveAMsgDTO>> getAllLeaveAMsg(
         @PathVariable("userId") Integer userId){
        List<LeaveAMsgDTO> leaveAMsg = leaveAMessageService.getAllLeaveAMsg(0,userId);
        return Result.successHaveData(leaveAMsg);
    }


    @PostMapping("/photo")
    @ApiOperation("留言图片上传")
    public Result<FileDto> upload(
            @RequestPart("photo") MultipartFile photo
    ) throws IOException {
        UUID uuid = UUID.randomUUID();
        String fileName = uuid.toString();
        String fileUrl =null;
        if(photo != null){
            if(!photo.isEmpty()){
                String originalFilename = photo.getOriginalFilename();
                String[] split = originalFilename.split("\\.");
                fileName = fileName+"."+split[1];
                // 上传到阿里云OSS
                fileUrl = AliYunOssUtil.upload(photo.getBytes(), fileName);
            }
        }
        FileDto fileDto = new FileDto(fileName,fileUrl);
        return Result.successHaveData(fileDto);
    }

    @DeleteMapping("/photo")
    @ApiOperation("留言图片删除")
    public Result<String> deletePhoto(
            @RequestParam("fileName") String fileName
    )  {
        AliYunOssUtil.DeleteOne(fileName);
        return Result.successNoData();
    }

    @PostMapping("/like/{LAMId}/{userId}")
    @ApiOperation("留言点赞")
    public Result<List<String>> cLikeComment(
            @PathVariable("LAMId") Integer LAMId,
            @PathVariable("userId") Integer userId)
    {
        likeService.likeLAM(LAMId, userId);
        return Result.successNoData();
    }
    @DeleteMapping("/unlike/{LAMId}/{userId}")
    @ApiOperation("取消评论点赞")
    public Result<List<String>> unlikeComment(
            @PathVariable("LAMId") Integer LAMId,
            @PathVariable("userId") Integer userId)
    {
        likeService.unlikeLAM(LAMId, userId);
        return Result.successNoData();
    }
}
